﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Xml.Linq;

namespace ProcessFile
{
    class Program
    {
        public  static string ConvertFilestk4Ad14(string filePath)
        {

            StkFile file = new StkFile(filePath);
            string content = "";
            string[] lines = file._lines;
            int dieliectricCount = 0;
            int topLayer = file.TopLayer;
            int topSolder = file.TopSolder;
            int bottomLayer = file.BottomLayer;
            int bottomSolder = file.BottomSolder;
            int id = Common.StartIdSeq;
            string Unit = Common.GetUnit(lines);
            ConvertStk4StackupFile convertStk4StackupFile = new ConvertStk4StackupFile();
            convertStk4StackupFile.Id0 = id + 1;
            convertStk4StackupFile.Id1 = id +2;
            content += convertStk4StackupFile.Hearder;

            string subStackId = convertStk4StackupFile.SubId;
            int indexLayer = 0;
                 
            for (int i = 0; i <= Math.Max(file.BottomSolder, file.BottomLayer); i++)
            {
                string[] item = lines[i].Split('^');
                if (item.Length >= 7)
                {
                    id++;
                    if (Common.IsDielectric(item[Common.DescriptionIndex]) && i != topSolder && i != bottomSolder)
                    {
                        dieliectricCount++;
                    }
                   
                   
                    ConvertStk4StackupLayerRow convertStk4Stackup = new ConvertStk4StackupLayerRow(lines[i],indexLayer);
                    convertStk4Stackup.SubstacksID = new string[]{filePath};
                    convertStk4Stackup.Unit = Unit;
                    convertStk4Stackup.DieliectricIndex = dieliectricCount;
                    if (i == topLayer) convertStk4Stackup.IsTopLayer = true;
                    if (i == topSolder) convertStk4Stackup.IsTopSolder = true;
                    if (i == bottomLayer) convertStk4Stackup.IsBottomLayer = true;
                    if (i == bottomSolder) convertStk4Stackup.IsBottomSolder = true;
                  content+=convertStk4Stackup.ToString();
                  indexLayer++;
                }

            }
            content += convertStk4StackupFile.Footer(indexLayer+2, id);
            content = "|"+content.Replace("\r\n", "|");
            return content;
        }
    public static string  GetLayerName(string input)
    {
        string pattern = @"NAME=(?<layername>[A-z|0-9|\s]*)\|";
            Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        if (match.Success)
        {
            return match.Groups["layername"].Value;
        }
        return "";
    }
    public static string GetDielectricType(string input)
    {
        string pattern = @"DIELTYPE=(?<layertype>[A-z|0-9|\s]*)\|";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        if (match.Success)
        {
            return match.Groups["layertype"].Value;
        }
        return "";
    }
    public static string GetDielectricConst(string input)
    {
        string pattern = @"DIELCONST=(?<layerconst>[A-z|0-9|\s|\.]*)\|";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        if (match.Success)
        {
            return match.Groups["layerconst"].Value;
        }
        return "";
    }
    public static string GetDielectricHeight(string input)
    {
        string pattern = @"DIELHEIGHT=(?<height>[0-9|\s|\.]*)";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        if (match.Success)
        {
            return match.Groups["height"].Value;
        }
        return "";
    }
    public static string GetDielectricCopThick(string input)
    {
        string pattern = @"COPTHICK=(?<height>[0-9|\s|\.]*)";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        if (match.Success)
        {
            return match.Groups["height"].Value;
        }
        return "";
    }
    public static string GetDielectricMaterial(string input)
    {
        string pattern = @"DIELMATERIAL=(?<height>[A-z|0-9|\s|]*)\|";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        if (match.Success)
        {
            return match.Groups["height"].Value;
        }
        return "";
    }
        public  static  void ParsingDielectricItem(string item)
        {
            
        }
        public  void SplipItemAD14(string item)
        {
            
        }
        public static DataSet ReadXml(string path)
        {
          
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(path);
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        static void Main(string[] args)
        {
            string xmlPath = @"C:\Users\tuyenvt\Downloads\IPC-2581 samples\All 6 Layer IPC-2581.xml";
            Stk4PIC2581 abc= new Stk4PIC2581(@"C:\Users\tuyenvt\Downloads\IPC-2581 samples\All 6 Layer.stk",xmlPath);
            string df =abc.GetTg("Tg=210C; Df=0.06");
            string rc = abc.GetRc("Megtron 4; 1080; Rc= 73% (4GHz)");
            string gs = abc.GetGLASS_STYLE("Megtron 4; 1080; Rc=73% (4GHz)");
            string aaa = abc.Convert4Xml;
            //XDocument patternDoc = XDocument.Load(xmlPath);
            //XNamespace ns = patternDoc.Root.Name.Namespace;
            //var elements = from c in patternDoc.Descendants(ns + "StackupLayer")
            //               select c;
            //foreach (var element in elements)
            //{
            //    string contains = element.Elements(ns + "NonstandardAttribute").Where(x => x.Attribute("name").Value == "LAYER_DIELECTRIC_CONSTANT").FirstOrDefault().Attribute("value").Value;
            //    Console.WriteLine("Element : " + element.Attribute("layerOrGroupRef").Value + " contains " + contains);
            //}
            Console.ReadKey();
          
        }
    }
}
